package com.example.foo.account.service;

import java.math.BigDecimal;

import org.springframework.transaction.annotation.Transactional;

import com.example.foo.account.model.Account;

@org.springframework.stereotype.Service
public class AccountTransferService {

	private final AccountService accountService;

	public AccountTransferService(AccountService accountService) {
		this.accountService = accountService;
	}

	// ========== AccountTransfer ==========

	@Transactional(timeout = 60, rollbackFor = Exception.class)
	public void createAccountTransfer(Account.Acctc acctc, String name, BigDecimal value, Long fromUserId, Long toUserId) {
		if (fromUserId.equals(toUserId)) {
			throw new IllegalArgumentException("fromUserId must not be equal to toUserId");
		}

		String relType = "Transfer";
		Long relId = 0L;

		accountService.updateAccountDecrement(acctc, name, value, fromUserId, toUserId, relType, relId);
		accountService.updateAccountIncrement(acctc, name, value, toUserId, fromUserId, relType, relId);
	}

}
